﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 物品槽内的物品
/// </summary>
public class ArticleItem : MonoBehaviour {

    private ArticleInfo _articleInfo = new ArticleInfo();
    public ArticleInfo ArticleInfo {
        get {
            return _articleInfo;
        }
    }

    private Image _image;
    /// <summary>
    /// 物品图标
    /// </summary>
    private Image Image {
        get {
            if (this._image == null) {
                this._image = this.GetComponent<Image>();
            }
            return this._image;
        }
    }
    private Text _amountText;
    /// <summary>
    /// 物品数量显示
    /// </summary>
    private Text AmountText {
        get {
            if (this._amountText == null) {
                this._amountText = this.GetComponentInChildren<Text>();
            }
            return this._amountText;
        }
    }
    private CanvasGroup _canvasGroup;
    private CanvasGroup CanvasGroup {
        get {
            if (this._canvasGroup == null) {
                this._canvasGroup = this.GetComponent<CanvasGroup>();
            }
            return this._canvasGroup;
        }
    }
    private float TargetAlpha = 1;
    public bool IsPickUpAll { get; private set; }

    // Use this for initialization
    void Start () {
          
    }
	
	// Update is called once per frame
	void Update () {
        UIHelper.HandleShowHide(this.CanvasGroup, this.TargetAlpha);
    }

    /// <summary>
    /// 存放物体。
    /// </summary>
    /// <param name="article">物品</param>
    /// <param name="amount">数量</param>
    /// <returns>溢出的数量</returns>
    public int SetArticle(Article article, int amount = 1) {
        this.ArticleInfo.Article = article;
        this.ArticleInfo.Amount = amount;

        int spaceNum = article.ArticleTemplet.SpaceNum;
        int overflow = this.ArticleInfo.Amount - spaceNum;
        if (overflow <= 0) { // 没有溢出
            // 不处理
        } else { // 溢出了
            this.ArticleInfo.Amount = spaceNum;
        }

        this.Image.sprite = Resources.Load<Sprite>(article.ArticleTemplet.Sprite);
        this.AmountText.text = spaceNum == 1 ? "" : this.ArticleInfo.Amount.ToString();

        return overflow;
    }

    /// <summary>
    /// 增加物品数量
    /// </summary>
    /// <param name="amount">数量</param>
    /// <returns>溢出的数量</returns>
    public int AddAmount(int amount) {
        this.ArticleInfo.Amount += amount;

        int spaceNum = this.ArticleInfo.Article.ArticleTemplet.SpaceNum;
        int overflow = this.ArticleInfo.Amount - spaceNum;
        if (overflow <= 0) { // 没有溢出
            // 不处理
        } else { // 溢出了
            this.ArticleInfo.Amount = spaceNum;
        }
        this.AmountText.text = this.ArticleInfo.Amount.ToString();
        return overflow;
    }

    /// <summary>
    /// 减少物品数量，返回剩余的数量
    /// </summary>
    /// <param name="amount">数量</param>
    /// <returns>剩余的数量</returns>
    public int ReduceAmount(int amount) {
        this.ArticleInfo.Amount -= amount;
        if (this.ArticleInfo.Amount < 0) {
            this.ArticleInfo.Amount = 0;
        }
        if (this.ArticleInfo.Amount == 0) {
            this.ArticleInfo.Article = null;
        }
        this.AmountText.text = this.ArticleInfo.Amount.ToString();
        return this.ArticleInfo.Amount;
    }

    public void PickUpAll() {
        this.IsPickUpAll = true;
        this.TargetAlpha = 0.5f;
    }

    public void UnPickUpAll() {
        this.IsPickUpAll = false;
        this.TargetAlpha = 1;
    }

    public ArticleSlot GetArticleSlot() {
        return this.GetComponentInParent<ArticleSlot>();
    }
}
